CMAKE_MINIMUM_REQUIRED(VERSION 3.12.0)

PROJECT(WonderTrader)

# 设置全局C++标准
SET(CMAKE_CXX_STANDARD 17)
SET(CMAKE_CXX_STANDARD_REQUIRED ON)
SET(CMAKE_CXX_EXTENSIONS OFF)

# 设置测试覆盖率选项（必须在添加子目录之前设置）
IF(CMAKE_BUILD_TYPE STREQUAL "Debug")
    SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --coverage -O0 -fprofile-arcs -ftest-coverage")
    SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} --coverage -O0 -fprofile-arcs -ftest-coverage")
    SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --coverage")
    MESSAGE(STATUS "Coverage flags enabled for Debug build")
ENDIF()

#3. 环境判断
IF (WIN32)
	MESSAGE(STATUS "Operation System is windows")
ELSEIF (UNIX)
	MESSAGE(STATUS "Operation System is UNIX-like OS's")
ENDIF ()

MESSAGE(STATUS "Generator is ${CMAKE_GENERATOR}")

IF (MSVC)
	ADD_DEFINITIONS (-D_CRT_SECURE_NO_WARNINGS)
	IF(CMAKE_CL_64)
		SET(INCS $ENV{MyDepends141}/include)
		SET(LNKS $ENV{MyDepends141}/lib/x64)

		MESSAGE(STATUS "MyDepends is at $ENV{MyDepends141}")
		MESSAGE(STATUS "Platform is x64")
		SET(PLATFORM x64)
	ELSE()
		SET(INCS $ENV{MyDepends141}/include)
		SET(LNKS $ENV{MyDepends141}/lib/x86)

		MESSAGE(STATUS "MyDepends is at $ENV{MyDepends141}")
		MESSAGE(STATUS "Platform is x86")
		SET(PLATFORM x86)
	ENDIF()
	SET(PREFIX )
	SET(SUFFIX .dll)
ELSE (GNUCC)
	IF(WIN32)
		SET (MyDeps $ENV{MyDependsGcc})
		SET(SUFFIX .dll)

		MESSAGE(STATUS "MyDepends is at $ENV{MyDependsGcc}")
	ELSEIF(APPLE)
		# macOS specific configuration
		SET(SUFFIX .dylib)
		# Use Homebrew paths for dependencies
		SET(HOMEBREW_PREFIX "/opt/homebrew")
		IF(NOT EXISTS ${HOMEBREW_PREFIX})
			SET(HOMEBREW_PREFIX "/usr/local")
		ENDIF()
		
		SET(MyDeps ${HOMEBREW_PREFIX})
		SET(INCS 
			${HOMEBREW_PREFIX}/include
			${HOMEBREW_PREFIX}/opt/boost/include
			${HOMEBREW_PREFIX}/opt/openssl/include
		)
		SET(LNKS 
			${HOMEBREW_PREFIX}/lib
			${HOMEBREW_PREFIX}/opt/boost/lib
			${HOMEBREW_PREFIX}/opt/openssl/lib
		)
		
		MESSAGE(STATUS "MyDepends is at ${MyDeps}")
		MESSAGE(STATUS "Using macOS/Homebrew dependencies")
	ELSE(UNIX)
		SET (MyDeps "/home/mydeps")
		SET(SUFFIX .so)

		MESSAGE(STATUS "MyDepends is at /home/mydeps")
	ENDIF()

	IF(NOT APPLE)
		SET(INCS ${MyDeps}/include)
		SET(LNKS ${MyDeps}/lib)
	ENDIF()

	MESSAGE(STATUS "Platform is x64")
	SET(PLATFORM x64)
	SET(PREFIX lib)
ENDIF()

# Find fmt library for Unix-like systems
IF(UNIX AND NOT APPLE)
	find_package(PkgConfig REQUIRED)
	pkg_check_modules(FMT REQUIRED fmt)
	link_libraries(${FMT_LIBRARIES})
ENDIF()

# ========================================
# 并行编译依赖关系优化
# ========================================

# 第一层：基础库 (必须最先构建)
ADD_SUBDIRECTORY(WTSUtils)
ADD_SUBDIRECTORY(WTSTools)

# 第二层：独立模块组 (可并行构建)
# Contract Loaders
ADD_SUBDIRECTORY(CTPLoader)
ADD_SUBDIRECTORY(CTPOptLoader)
ADD_SUBDIRECTORY(MiniLoader)

# Parsers (数据解析器)
ADD_SUBDIRECTORY(ParserCTP)
ADD_SUBDIRECTORY(ParserCTPMini)
ADD_SUBDIRECTORY(ParserCTPOpt)
ADD_SUBDIRECTORY(ParserFemas)
ADD_SUBDIRECTORY(ParserXTP)
ADD_SUBDIRECTORY(ParserXeleSkt)
ADD_SUBDIRECTORY(ParserUDP)

# Traders (交易接口)
ADD_SUBDIRECTORY(TraderCTP)
ADD_SUBDIRECTORY(TraderCTPMini)
ADD_SUBDIRECTORY(TraderCTPOpt)
ADD_SUBDIRECTORY(TraderFemas)
ADD_SUBDIRECTORY(TraderMocker)
ADD_SUBDIRECTORY(TraderXTP)
ADD_SUBDIRECTORY(TraderYD)

# Data Storage
ADD_SUBDIRECTORY(WtDataStorage)
ADD_SUBDIRECTORY(WtDataStorageAD)

# 第三层：核心模块 (依赖基础库)
ADD_SUBDIRECTORY(WtDtCore)
ADD_SUBDIRECTORY(WtBtCore)
ADD_SUBDIRECTORY(WtCore)
ADD_SUBDIRECTORY(WtUftCore)

# 第四层：支持模块 (依赖核心模块)
ADD_SUBDIRECTORY(WtDtHelper)
ADD_SUBDIRECTORY(WtExeFact)
ADD_SUBDIRECTORY(WtRiskMonFact)
ADD_SUBDIRECTORY(WtMsgQue)
ADD_SUBDIRECTORY(WtUftStraFact)

# 第五层：Porter模块 (依赖多个模块)
ADD_SUBDIRECTORY(WtDtPorter)
ADD_SUBDIRECTORY(WtDtServo)
ADD_SUBDIRECTORY(WtBtPorter)
ADD_SUBDIRECTORY(WtPorter)
ADD_SUBDIRECTORY(WtExecMon)

# 第六层：可执行程序 (依赖所有相关模块)
ADD_SUBDIRECTORY(QuoteFactory)
ADD_SUBDIRECTORY(WtBtRunner)
ADD_SUBDIRECTORY(WtRunner)
ADD_SUBDIRECTORY(WtUftRunner)

# 第七层：性能测试工具 (最后构建)
ADD_SUBDIRECTORY(WtLatencyHFT)
ADD_SUBDIRECTORY(WtLatencyUFT)

#test projects
# ADD_SUBDIRECTORY(TestBtPorter)
# ADD_SUBDIRECTORY(TestDtPorter)
# ADD_SUBDIRECTORY(TestExecPorter)
# ADD_SUBDIRECTORY(TestPorter)
# ADD_SUBDIRECTORY(TestTrader)
# ADD_SUBDIRECTORY(TestParser)
# ADD_SUBDIRECTORY(TestUnits)

# Only used on win
# IF(MSVC)
# ADD_SUBDIRECTORY(TestTrader)
# ENDIF()

# Enable testing
ENABLE_TESTING()

# 添加测试目录
# ADD_SUBDIRECTORY(../tests tests)

